Passed
Pull Request — master (#112)
by Mathieu
01:45
created

HolidayToEventsConverter.convert   B

Complexity

Conditions 5

Size

Total Lines 29
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 25
dl 0
loc 29
rs 8.8133
c 0
b 0
f 0
cc 5
1
import {Inject} from '@nestjs/common';
2
import {
3
  Holiday,
4
  HolidayLeaveType
5
} from '../../HumanResource/Holiday/Holiday.entity';
6
import {Event, EventType} from 'src/Domain/FairCalendar/Event.entity';
7
import {IEventRepository} from '../Repository/IEventRepository';
8
import {IDateUtils} from 'src/Application/IDateUtils';
9
10
export class HolidayToEventsConverter {
11
  constructor(
12
    @Inject('IEventRepository')
13
    private readonly eventRepository: IEventRepository,
14
    @Inject('IDateUtils')
15
    private readonly dateUtils: IDateUtils
16
  ) {}
17
18
  public convert(holiday: Holiday): void {
19
    const user = holiday.getUser();
20
    const type =
21
      holiday.getLeaveType() === HolidayLeaveType.MEDICAL
22
        ? EventType.MEDICAL_LEAVE
23
        : EventType.HOLIDAY;
24
25
    const dates = this.dateUtils.getWorkedDaysDuringAPeriod(
26
      new Date(holiday.getStartDate()),
27
      new Date(holiday.getEndDate())
28
    );
29
30
    if (!dates) {
31
      return;
32
    }
33
34
    const firstDate = dates[0].toISOString();
35
    const lastDate = dates[dates.length - 1].toISOString();
36
37
    for (const date of dates) {
38
      const currentDate = date.toISOString();
39
      const time =
40
        (firstDate === currentDate && false === holiday.isStartsAllDay()) ||
41
        (lastDate === currentDate && false === holiday.isEndsAllDay())
42
          ? 50
43
          : 100;
44
45
      this.eventRepository.save(new Event(type, user, time, currentDate));
46
    }
47
  }
48
}
49